Check Spatial Autocorrelation of residuals

ArcGIS PRO - R


In [ ]:
###### Importing ####
library(arcgisbinding)
library(sp)
library(spdep)

In [ ]:
#### Initialize arcgisbinding ####
arc.check_product()

In [ ]:
##### Current workspace 
getwd()

#### Input Variables ####
fullPath = '../data'
inputFC = file.path(fullPath, "pysal_automodel.shp")
galFile = file.path(fullPath,"queen.gal")
uniqueField  = 'MYID'
residualField = 'RESID'

In [ ]:
### Loading dataset
info <- arc.open(inputFC)
#### Create Data.Frame ####
df <- arc.select(info, c(uniqueField, residualField))
head(df)

In [ ]:
#### Spatial Data Object####
spObject <- arc.data2sp(df)

#### Plot Spatial Data #### 
spplot(spObject[residualField])

In [ ]:
#### Loading Spatial Dependence Weight Matrix ####
gal  <- read.gal(galFile, df[[uniqueField]])

#### Weights #####
col.W <- nb2listw(gal, style="W")
col.W

In [ ]:
### Calculate Moran ####
  moranI = moran.test(df[[residualField]], col.W)
  print( paste("Moran's I:" , moranI$statistic))
  print( paste("Moran's I p-value:" , moranI$p.value))

In [ ]:
### Moran plot ###
moran.plot(df[[residualField]], col.W)